<?php
namespace Admin\Controller;
use Think\Controller;
class BillController extends CommonController {

	protected $table = 'Bill';

	/**
	 * 初始化
	 */
	public function __construct() {
		parent::__construct();
	}

	/**
	 * 列表
	 */
    public function index(){
		$id = I('get.id','','intval');
		$this->assign('id',$id);
		$where = 'data_student on data_bill.bsid = data_student.id';
		$order = 'bid desc';
		$this->pagejoin($this->table, $where, $order, 20); // 分页

		$subject = D('Subject')->lists(); // 学科
		$this->assign('subject',$subject);

		$class = D('Class')->lists(); // 班级
		$this->assign('class',$class);

		$this->display();
    }
	
	/**
	 * 缴费
	 */
	public function add() {
		if($_POST) {
			$where['ssid'] = I('post.bsid','','intval');
			$where['scid'] = I('post.bcid','','intval');
			$check = D('class_student')->where($where)->find();
			if(!empty($check)) {
				$this->error('该学员已经在该班中！');
			}
			
			$admin = D($this->table);
			$checkmsg = $admin->checkform($_POST);
			if(!empty($checkmsg)) { // 表单验证
				$this->error($checkmsg);
			} else { // 表单处理
				$msg = $admin->handleform($_POST);
				if(empty($msg)) {
					$this->error('操作失败！');
				} else {
					$data['ssid'] = I('post.bsid','','intval');
					$data['scid'] = I('post.bcid','','intval');
					$info = D('class')->find($data['scid']);
					$data['stime'] = time();
					$data['snum'] = $info['crest'];
					$msgclass = D('class_student')->add($data);

					$this->error('操作成功！',U($this->table.'/info',array('id'=>$msg)));
				}
			}
			die();
		}
		$id = I('get.id','','intval');
		$info = D('student')->find($id); // 学员信息
		$this->assign('info',$info);

		$subject = D('Subject')->lists(); // 学科
		$this->assign('subject',$subject);

		$class = D('Class')->lists(); // 班级
		$this->assign('class',$class);
		$this->assign('classm',$class);
		$this->display();
	}

	public function info() {
		$id = I('get.id','','intval');
		$bill = D('Bill')->find($id);
		$this->assign('bill',$bill);

		$info = D('Student')->find($bill['bsid']);
		$this->assign('info',$info);

		$lesson = $this->listless($bill['bcid']);
		$this->assign('class',$lesson['info']);
		//$this->assign('list',$lesson['list']);
		$this->assign('lesson',$lesson['lesson']);

		$subject = D('Subject')->find($lesson['info']['ctype']);
		$this->assign('subject',$subject);

		$this->display();
	}

	/**
	 * 删除
	 */
	public function del() {
		$id = I('get.id','','intval');
		$msg = D($this->table)->delete($id);
		if(empty($msg)) {
			$this->error('删除失败！');
		} else {
			$this->success('删除成功！',U($this->table.'/index'));
		}
	}

}
